package vmcs.ui.interfacecontrol;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import vmcs.common.Constant;
import vmcs.controller.*;
import vmcs.customer.CoinReceiver;
import vmcs.simulationsystem.*;

public class JavaCustomerPanelBuilder extends CustomerPanelBuilder {
	
    private static final String TITLE = "VMCS - Customer Panel";  
    public final static Color ACT_COLOR = Color.white;
    private CustomerSimulationPanel customerPanel = null;
    
	public JavaCustomerPanelBuilder(ISimulatorPanel startUpPanel,
			TransactionController transactionController) {
		super(startUpPanel, transactionController);
		this.customerPanel = new CustomerSimulationPanel();
	}
	
	@Override
	public void buildPanel() {
		CoinReceiver coinRcv = transactionController.getCoinReceiver();
		this.customerPanel.setCoinInput(new CoinInputBox(this.customerPanel.getDialog(), coinRcv));
		this.customerPanel.getCoinInput().setActive(false);
		this.customerPanel.getCoinInput().addListener();
		
		this.customerPanel.setDrinkInput(new DrinkSelectionBox(this.customerPanel.getDialog(), transactionController));
		this.customerPanel.getDrinkInput().setAction();
        
		Label lb = new Label("VIMTO Soft Drink Dispenser");
        lb.setFont(new Font("Helvetica", Font.BOLD, 24));
        lb.setAlignment(Label.CENTER);
        
        Panel tl1 = new Panel();
        tl1.add(lb);
        
        Panel tl3 = new Panel(new GridLayout(3,1));
        this.customerPanel.getCoinInput().setActive(false);
        tl3.add(this.customerPanel.getCoinInput());
        tl3.add(this.customerPanel.getInvalidCoin());
        tl3.add(this.customerPanel.getMoneyInserted());

        Panel tp1 = new Panel();
        tp1.setLayout(new GridLayout(1,1));
        tp1.add(this.customerPanel.getDrinkInput());

        this.customerPanel.setTerminateBtn(new Button("Terminate and Return Cash"));
        this.customerPanel.getTerminateBtn().setFont(new Font("Helvetica", Font.BOLD, 12));
        this.customerPanel.getTerminateBtn().setBackground(ACT_COLOR);
        Panel tp3 = new Panel();
        tp3.setLayout(new FlowLayout());
        tp3.add(this.customerPanel.getTerminateBtn());
        
		ActionListenerFactory panelButtonListenerFactory = new ActionListenerFactory(transactionController.getMainController());
		this.customerPanel.getTerminateBtn().addActionListener(panelButtonListenerFactory.getButtonListener(Constant.CUST_TR_TRANS));
        //this.customerPanel.getTerminateBtn().addActionListener(new TerminateButtonListener(transactionController));
        
        Panel tp2 = new Panel();
        tp2.setLayout(new GridLayout(2,1));
        tp2.add(this.customerPanel.getNoChangeAvail());
        tp2.add(tp3);
        
        Panel tp4 = new Panel();
        tp4.setLayout(new BorderLayout());
        tp4.add("North", this.customerPanel.getCollectCoin());
        tp4.add("Center",this.customerPanel.getCollectCan());

        Panel tpn = new Panel(new BorderLayout());
        tpn.add("North", tl1);
        tpn.add("Center", tl3);
        tpn.add("South", tp1);
        
        Panel tpc = new Panel(new GridLayout(2, 1));
        tpc.add(tp2);
        tpc.add(tp4);
        
		Dialog dialog = new Dialog(((SimulatorControlPanel)this.startUpPanel).getFrame(), TITLE, false);
        dialog.setLayout(new BorderLayout());
        dialog.add("North", tpn);
        dialog.add("Center", tpc);
        dialog.pack();

        dialog.setLocation(200,100);
        
        dialog.addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					transactionController.closeDown();
                }
        });

		this.customerPanel.setDialog(dialog);
	}
	
	@Override
	public void buildDrinkSelection() {
		
	}

	@Override
	public void buildCanCollector() {
		this.customerPanel.setCollectCan(new LabelledDisplay("Collect Can Here:", 10, LabelledDisplay.FLOW));
		this.customerPanel.getCollectCan().setEditable(false);
		this.customerPanel.getCollectCan().setTextBackground(Color.BLACK);
		this.customerPanel.getCollectCan().setTextForeground(Color.WHITE);
	}

	@Override
	public void buildCoinCollector() {        
		this.customerPanel.setCollectCoin(new LabelledDisplay("Collect Coins:   ",  5, LabelledDisplay.FLOW));
		this.customerPanel.getCollectCoin().setEditable(false);
		this.customerPanel.getCollectCoin().setTextBackground(Color.BLACK);
		this.customerPanel.getCollectCoin().setTextForeground(Color.WHITE);
	}

	@Override
	public void buildCoinInputBox() {
		this.customerPanel.setMoneyInserted(new LabelledDisplay("Total Money Inserted:", 10, LabelledDisplay.DEFAULT));
		this.customerPanel.getMoneyInserted().setEditable(false);
		this.customerPanel.getMoneyInserted().setTextBackground(Color.BLACK);
		this.customerPanel.getMoneyInserted().setTextForeground(Color.WHITE);
	}

	@Override
	public void buildMessagePanel() {
		this.customerPanel.setInvalidCoin(new WarningDisplay("Invalid Coin"));
		this.customerPanel.setNoChangeAvail(new WarningDisplay("No Change Available"));
	}

	@Override
	public ICustomerPanel getCustomerPanel() {
		return customerPanel;
	}

}
